package org.ckhd.calculate24;

import org.apache.commons.math3.fraction.Fraction;

/**
 * 分数类
 */
public class MyFraction {
    /**
     * 分数。直接使用已有的分数类：commons.math3.fraction.Fraction，可以不用考虑四则运算的细节，简化代码
     */
    public Fraction fraction;
    /**
     * 来源，即这个数通过哪两个数计算得到
     */
    public Source source;

    public MyFraction(int num) {
        fraction = new Fraction(num);
    }

    public MyFraction(int num, int den) {
        fraction = new Fraction(num, den);
    }

    public MyFraction(Fraction fraction, Source source) {
        this.fraction = fraction;
        this.source = source;
    }

    /**
     * 加
     */
    public MyFraction add(MyFraction f) {
        return new MyFraction(fraction.add(f.fraction), new Source(Constants.SIGN_ADD, this, f));
    }

    /**
     * 减
     */
    public MyFraction subtract(MyFraction f) {
        return new MyFraction(fraction.subtract(f.fraction), new Source(Constants.SIGN_SUBTRACT, this, f));
    }

    /**
     * 乘
     */
    public MyFraction multiply(MyFraction f) {
        return new MyFraction(fraction.multiply(f.fraction), new Source(Constants.SIGN_MULTIPLY, this, f));
    }

    /**
     * 除
     */
    public MyFraction divide(MyFraction f) {
        return new MyFraction(fraction.divide(f.fraction), new Source(Constants.SIGN_DIVIDE, this, f));
    }

    /**
     * 比较大小
     */
    public int compareTo(MyFraction myFraction) {
        return fraction.compareTo(myFraction.fraction);
    }

    /**
     * 比较顺序
     */
    public int compareToByIndex(MyFraction f) {
        if (this.compareTo(f) == 0) {
            return this.getSourceStr().compareTo(f.getSourceStr());
        } else {
            return this.compareTo(f);
        }
    }

    /**
     * 获取一个数的表达式
     */
    public String getSourceStr() {
        if (source == null) {
            return String.valueOf(fraction.intValue());
        } else {
            if (source.sign.equals(Constants.SIGN_ADD) || source.sign.equals(Constants.SIGN_SUBTRACT)) {//加减
                return new StringBuilder().append(source.f1.getSourceStr()).append(source.sign).append(source.f2.getSourceStr()).toString();
            } else {//乘除
                if (source.f1.source != null && (Constants.SIGN_ADD.equals(source.f1.source.sign) || Constants.SIGN_SUBTRACT.equals(source.f1.source.sign))) {
                    if (source.f2.source != null && (Constants.SIGN_ADD.equals(source.f2.source.sign) || Constants.SIGN_SUBTRACT.equals(source.f2.source.sign))) {//f1、f2的来源都为加减
                        return new StringBuilder().append("(").append(source.f1.getSourceStr()).append(")").append(source.sign).append("(").append(source.f2.getSourceStr()).append(")").toString();
                    } else {//f1来源为加减，f2不是
                        return new StringBuilder().append("(").append(source.f1.getSourceStr()).append(")").append(source.sign).append(source.f2.getSourceStr()).toString();
                    }
                } else {
                    if (source.f2.source != null && (Constants.SIGN_ADD.equals(source.f2.source.sign) || Constants.SIGN_SUBTRACT.equals(source.f2.source.sign))) {//f2来源为加减，f1不是
                        return new StringBuilder().append(source.f1.getSourceStr()).append(source.sign).append("(").append(source.f2.getSourceStr()).append(")").toString();
                    } else {//f1、f2的来源都不为加减
                        return new StringBuilder().append(source.f1.getSourceStr()).append(source.sign).append(source.f2.getSourceStr()).toString();
                    }
                }
            }
        }
    }

}
